package com.glory.dmegcpda.viewmodel

import androidx.databinding.ObservableArrayList
import com.glory.dmegcpda.R
import com.glory.dmegcpda.bean.MLotListBean
import com.glory.dmegcpda.model.MaterialReceiveRepository
import com.glory.mvvmnet.base.BaseViewModel
import com.glory.mvvmnet.entity.ToastMessageBean
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import me.tatarka.bindingcollectionadapter2.BR
import me.tatarka.bindingcollectionadapter2.ItemBinding
import java.io.UnsupportedEncodingException

/**
 * FileName: MaterialReceiveViewModel
 * Author: XJH
 * Date: 2021/9/27 15:20
 * Description:
 * History:
 */
class MaterialReceiveViewModel : BaseViewModel() {

    val materialTitle = arrayListOf("物料号","批号","物料规格", "数量")
    var getMaterialList = arrayListOf<String>()
    var putMaterialList = arrayListOf<String>()
    val materialReceiveRepository by lazy { MaterialReceiveRepository() }

    //物料列表
    var mLotItems = ObservableArrayList<MLotListBean>()
    var mMaterielBinding =
        ItemBinding.of<MLotListBean>(BR.mLotItem, R.layout.item_mlot)


    fun getPopInfo(){
        val map = JsonObject()
        map.addProperty("ACTIONTYPE","GETBYREFNAME")
        map.addProperty("REFNAME","PVMRequisitionMode")
        val map2 = JsonObject()
        map2.addProperty("ACTIONTYPE","GETBYREFNAME")
        map2.addProperty("REFNAME","PVMWMSWarehouse")
        launchOnlyResult({
            val popInfo = materialReceiveRepository.getPopInfo(map) // 领料方式
            val popInfo1 = materialReceiveRepository.getPopInfo(map2)  //发料库位
            if (popInfo.ADUREFLIST.isNotEmpty()) {
                popInfo.ADUREFLIST.forEach {
                    getMaterialList.add(it.KEYID + "##" + it.DESCRIPTION)
                }
            }
            if (popInfo1.ADUREFLIST.isNotEmpty()){
                popInfo1.ADUREFLIST.forEach {
                    putMaterialList.add(it.KEYID + "##" + it.DESCRIPTION)
                }
            }
        })
    }

    fun submit(){
        val map = JsonObject()
        map.addProperty("ACTIONTYPE","RECEIVEININV")
        map.add("MLOTLIST", JsonParser().parse(Gson().toJson(mLotItems)).asJsonArray)
        launchOnlyResult({
            materialReceiveRepository.lotReceive(map)
            defUI.toastEvent.value = ToastMessageBean("物料接收成功",true)
            defUI.startActivity.call()
        })
    }

    fun addData(getM : String,putM : String,lot : String,partCode : String,num : String){
        if (mLotItems.isNotEmpty()){
            mLotItems.forEach {
                if (it.MATERIALNAME == lot.split("|")[0] && it.PARTNERLOTID == lot.split("|")[1]){
                    defUI.toastEvent.value = ToastMessageBean("当前绿码已经在列表中存在，请重新扫描绿码",false)
                    defUI.callObserve.call()
                    return
                }
            }
        }
        val entity  = MLotListBean()
        entity.MATERIALNAME = lot.trim().split("|")[0]
        entity.PARTNERLOTID = lot.trim().split("|")[1]
        entity.MATERIALSPECS = lot.split("|")[2]
        entity.TRANSMAINQTY = num
        entity.TRANSWAREHOUSEID = "INV_M3"
        entity.PARTNERCODE = partCode
        entity.RESERVED1 = getM.split("##")[0]
        entity.RESERVED2 = putM.split("##")[0]
        mLotItems.add(entity)
        defUI.callObserve.call()

    }



}